<?php

/**
 * This is the model class for table "usuario".
 *
 * The followings are the available columns in table 'usuario':
 * @property integer $id_rol_partido
 * @property string $username
 * @property string $password
 * @property integer $activo
 * @property string $creado
 * @property string $ultimaVisita
 *
 * The followings are the available model relations:
 * @property RolPartido $idRolPartido
 */
class Usuario extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Usuario the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'usuario';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_rol_partido, username, activo', 'required'),
			array('id_rol_partido, activo', 'numerical', 'integerOnly'=>true),
			array('username, password', 'length', 'max'=>50),
			array('creado, ultimaVisita', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_rol_partido, username, password, activo, creado, ultimaVisita', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'rol_partido' => array(self::BELONGS_TO, 'RolPartido', 'id_rol_partido'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_rol_partido' => 'Id Rol Partido',
			'username' => 'Username',
			'password' => 'Password',
			'activo' => 'Activo',
			'creado' => 'Creado',
			'ultimaVisita' => 'Ultima Visita',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_rol_partido',$this->id_rol_partido);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('activo',$this->activo);
		$criteria->compare('creado',$this->creado,true);
		$criteria->compare('ultimaVisita',$this->ultimaVisita,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function roles(){
		$sql='select tr.descripcion rol from tipo_rol tr
				inner join rol_grupo rg on rg.id_tipo_rol=tr.id
				inner join grupo g on g.id=rg.id_grupo
				inner join usuario_grupo ug on ug.id_grupo=g.id
				inner join usuario u on u.id_rol_partido=ug.id_usuario
				where u.username=:username';	
		$connection=Yii::app()->db; // assuming you have configured a "db" connection
		$command=$connection->createCommand($sql);
		$command->bindParam(':username',($this->username),PDO::PARAM_STR);		
		$rolesDB= $command->queryAll();
		$roles=array();
		foreach ($rolesDB as $role){
		  $roles[]=	$role['rol'];
		}
		return implode(',',$roles);
	}
	
	public function beforeSave(){
		if(parent::beforeSave()){
			$this->password=md5($this->password);
			return true;
		}		
		else{
			return false;
		}
	}
	
	
	
}